#include "../include/Simhash.hpp"

Deduplication::Deduplication(const map<string, string> &configMap)
    : _simhasher(configMap.at("DICT_PATH").c_str()
                , configMap.at("HMM_PATH").c_str()
                , configMap.at("IDF_PATH").c_str()
                , configMap.at("STOP_WORD_PATH").c_str())
{
}

vector<string> Deduplication::wordSeg(const string str, size_t topN)
{
    vector<string> res;
    _simhasher.extract(str, res, topN);
    return res;
}

vector<processedFile> Deduplication::deduplicate(vector<processedFile> &proFiles, map<int, uint64_t> u)
{
    vector<processedFile> newproFiles;
    for (int i = 0; i < proFiles.size(); ++i)
    {
        auto it = u.find(i);
        if (it == u.end())
        {
            continue;
        }
        else
        {
            newproFiles.push_back(proFiles.at(i));
        }
    }
    return newproFiles;
}

void Deduplication::operator()(vector<processedFile> &proFiles, size_t topN)
{
    map<int, uint64_t> u;
    int cnt = 0;

    for (auto file : proFiles)
    {
        uint64_t u64 = 0;
        // vector<pair<string ,double>> res;
        //_simhasher.extract(file.content, res, topN);
        _simhasher.make(file.content, topN, u64);
        if (cnt == 0)
        {
            u.insert(std::make_pair(0, u64));
        }
        else
        {
            int flag = 0;
            for (auto uint : u)
            {
                if (simhash::Simhasher::isEqual(uint.second, u64))
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                u.insert(std::make_pair(cnt, u64));
            }
        }
        ++cnt;
    }

    proFiles = deduplicate(proFiles, u);
}

/*
int main(int argc, char** argv)
{
    Simhasher simhasher("../include/cppjieba/dict/jieba.dict.utf8", "../include/cppjieba/dict/hmm_model.utf8", "../include/cppjieba/dict/idf.utf8", "../include/cppjieba/dict/stop_words.utf8");
    string s("我是蓝翔技工拖拉机学院手扶拖拉机专业的。不用多久，我就会升职加薪，当上总经理，出任CEO，走上人生巅峰。");
    size_t topN = 5;
    uint64_t u64 = 0;
    //vector<pair<string ,double> > res;
   // simhasher.extract(s, res, topN);
    simhasher.make(s, topN, u64);
    cout<< "文本：\"" << s << "\"" << endl;
    //cout << "关键词序列是: " << res << endl;
    cout<< "simhash值是: " << u64<<endl;


    const char * bin1 = "100010110110";
    const char * bin2 = "110001110011";
    uint64_t u1, u2;
    u1 = Simhasher::binaryStringToUint64(bin1);
    u2 = Simhasher::binaryStringToUint64(bin2);

    cout<< bin1 << "和" << bin2 << " simhash值的相等判断如下："<<endl;
    cout<< "海明距离阈值默认设置为3，则isEqual结果为：" << (Simhasher::isEqual(u1, u2)) << endl;
    cout<< "海明距离阈值默认设置为5，则isEqual结果为：" << (Simhasher::isEqual(u1, u2, 5)) << endl;
    return EXIT_SUCCESS;
}
*/
